Slack jenkins alert

Now we will create a notification to slack after building the project.

In slack workspace, go to Settings & administration - manage apps

Search Jenkins CI and add to slack.

Setup Instructions

Step 1

In your Jenkins dashboard, click on Manage Jenkinsfrom the left navigation.

Step 2

Click on Manage Pluginsand search for Slack Notificationin the Availabletab. Click the checkbox and install the plugin.

Step 3

After it's installed, click on Manage Jenkinsagain in the left navigation, and then go to Configure System. Find the Global Slack Notifier Settingssection and add the following values:

  • Team Subdomain: devsecops-rcm2595

  • Integration Token Credential ID: Create a secret text credential using BX2nTsWQSqRz4cu37UiVxzLj as the value

The other fields are optional. You can click on the question mark icons next to them for more information. Press Savewhen you're done.

Note:Please remember to replace the Integration Token in the screenshot below with your own.

We will create an entry in the workspace field as devsecops-rcm2595 this is the Team Subdomain provided after installing the jenkins app

Next we create a credential named slack-token with the secret text BX2nTsWQSqRz4cu37UiVxzLj which has been provided.

default channel is the channel to receive notifications on slack

Run test and we can have this message in slack

Step 4

For each Project that you would like receive notifications for, choose Configurefrom the project's menu.

Step 5

Then you'll need to add Slack Notificationsto the Post-build Actionsfor this project.

Step 6

In the Slack Notificationssection, choose the events you'd like to be notified about.

Now, we will create Shared Libraries to customize the message to send to slack.

  1. Go to your Jenkins dashboard and click on "Manage Jenkins".

  2. Click on "Configure System".

  3. Scroll down to the "Global Pipeline Libraries" section and click on the "Add" button.

  4. In the "Name" field, enter a name for your library (for example, "slack-library").

  5. In the "Default Version" field, enter the default version of your library (for example, "main").

  6. In the "Modern SCM" section, select the source code management system you are using for your library (for example, "Git").

  7. Enter the repository URL for your library in the "Project Repository" field (for example, " https://github.com/myusernam e/my-slack-library.git").

  8. Click on "Save" to save your changes.

Now, you can use your new global pipeline library in your Jenkins pipelines to send custom messages to Slack. You need add @Library('slack') _ on the top of jenkins pipeline and here is script.

@Library('slack') _
  always {
      sendNotification currentBuild.result
    }
  }

This is a Jenkinsfile that uses a shared library called ‘slack’ and sends a notification with the current build result.

The ‘@Library’ annotation is used to specify the name of the shared library to use. The underscore character (‘_’) specifies the default version of the library to use.

The ‘always’ block is a pipeline step that will always be executed regardless of whether the previous steps have failed or succeeded. It sends a notification with the current build result using the ‘sendNotification’ function.

The 'currentBuild.result' variable is used to access the result of the current build in Jenkins. It can be used to determine if there were any test failures or if the build was successful.

For example, in your Jenkinsfile, you are using the ‘sendNotification’ function to send a notification with the current build result. The ‘currentBuild.result’ variable is used to get the result of the current build and pass it as an argument to the ‘sendNotification’ function.

Now we create file sendNotification.groovy in vars folder to custom slack notification.

def call(String buildStatus = 'STARTED') {
 buildStatus = buildStatus ?: 'SUCCESS'
 def color
 if (buildStatus == 'SUCCESS') {
  color = '#47ec05'
 } else if (buildStatus == 'UNSTABLE') {
  color = '#d5ee0d'
 } else {
  color = '#ec2805'
 }
 def msg = "${buildStatus}: `${env.JOB_NAME}` #${env.BUILD_NUMBER}:\n${env.BUILD_URL}"
 slackSend(color: color, message: msg)
}

This is a Groovy script file that includes a function called "call". The function takes a string argument called "buildstatus" and if no value is specified, it defaults to "started".

The function then checks the value of "buildstatus" and assigns a color based on its value. If "buildstatus" is equal to "success", then the color "#47ec05" is assigned. If "buildstatus" is equal to "unstable", then the color "#d5ee0d" is assigned. Otherwise, the color "#ec2805" is assigned.

The function then creates a message string that contains the value of buildStatus, the name of the job (env.JOB_NAME), the build number (env.BUILD_NUMBER) and the build URL (env.BUILD_URL) using Groovy's string interpolation feature¹. Finally, it calls another function named slackSend with two parameters: color and message. The function sends a notification message to Slack with the specified color and message.

You can see result below

Reference: Extending with Shared Libraries (jenkins.io)

https://e.printstacktrace.blog/jenkins-pipeline-environment- variables-the-definitive-guide/

https://stackoverflow.com/questions/39140191/how-to-send-slack-notification-after-a-jenkins-pi peline-build-failed

Next, we go to with manual approve the Deployment to Production

stage('Prompte to PROD?') {
      steps {
        timeout(time: 2, unit: 'DAYS') {
          input 'Do you want to Approve the Deployment to Production Environment/Namespace?'
        }
      }
    }

This stage is a timeout step that waits for 2 days for a user input. The user input is asking for approval to deploy the code to the production environment or namespace. If the user approves the deployment within the timeout period, the pipeline continues to the next stage, which is likely a stage that triggers the actual deployment to production. If the user does not approve the deployment within the timeout period, the pipeline will fail because the deployment was not approved. This stage adds an important layer of safety to the pipeline to ensure that code changes are not accidentally or haphazardly deployed to production without proper review and approval.

Now run build and you can you notification like below

You can select proceed and have slack notification like below